/**
 * desc： 组件库相关接口
 * author：王文军
 * date：  2022/5/3 上午11:04
 */
"use strict";
const Controller = require("egg").Controller;
const mongo = require("../../utils/mongo");
const { success, failed } = require("../../utils/request");

class ComponentSiteController extends Controller {
  async index() {
    const { ctx } = this;
    const data = await mongo().query("componentSite");
    if (data && data.length > 0) {
      ctx.body = data[0];
    } else {
      ctx.body = {};
    }
  }
  async create() {
    const { ctx } = this;
    try {
      const res = await mongo().insert("componentSite", [ctx.request.body]);
      if (res.result.ok) {
        return (ctx.body = success("数据创建成功！"));
      }
      return (ctx.body = failed("数据创建失败，请稍后重试！"));
    } catch (e) {
      return (ctx.body = failed("系统出错，请稍后重试！"));
    }
  }
}

module.exports = ComponentSiteController;
